{% extends 'layout/mange.html' %}

{% block title %}{{ project.name }} - BUG详情{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col-md-8">
                            <h3 class="panel-title">{{ bug.title }}</h3>
                        </div>
                        <div class="col-md-4 text-right">
                            <a href="{% url 'bug_edit' project_id=project.id bug_id=bug.id %}" class="btn btn-primary btn-sm">
                                <i class="fa fa-edit"></i> 编辑
                            </a>
                            <button class="btn btn-danger btn-sm delete-btn" data-url="{% url 'bug_delete' project_id=project.id bug_id=bug.id %}">
                                <i class="fa fa-trash"></i> 删除
                            </button>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>状态</label>
                                <select class="form-control status-select" data-url="{% url 'bug_status_update' project_id=project.id bug_id=bug.id %}">
                                    {% for status in project.bugstatus_set.all %}
                                        <option value="{{ status.id }}" {% if status.id == bug.status.id %}selected{% endif %}>{{ status.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                            <div class="form-group">
                                <label>优先级</label>
                                <p class="form-control-static">{{ bug.priority.name }}</p>
                            </div>
                            <div class="form-group">
                                <label>创建人</label>
                                <p class="form-control-static">{{ bug.creator.username }}</p>
                            </div>
                            <div class="form-group">
                                <label>指派给</label>
                                <p class="form-control-static">{{ bug.assign_to.username|default:'未指派' }}</p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>创建时间</label>
                                <p class="form-control-static">{{ bug.create_datetime|date:'Y-m-d H:i' }}</p>
                            </div>
                            <div class="form-group">
                                <label>更新时间</label>
                                <p class="form-control-static">{{ bug.update_datetime|date:'Y-m-d H:i' }}</p>
                            </div>
                            <div class="form-group">
                                <label>截止日期</label>
                                <p class="form-control-static">{{ bug.deadline|date:'Y-m-d H:i'|default:'无' }}</p>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>描述</label>
                        <pre class="form-control-static">{{ bug.desc }}</pre>
                    </div>
                </div>
            </div>

            <!-- 评论区域 -->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">评论</h3>
                </div>
                <div class="panel-body">
                    <form method="post" novalidate>
                        {% csrf_token %}
                        <input type="hidden" name="comment" value="1">
                        <div class="form-group">
                            {{ comment_form.content }}
                            {% if comment_form.content.errors %}
                                <div class="text-danger">{{ comment_form.content.errors.0 }}</div>
                            {% endif %}
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">提交评论</button>
                        </div>
                    </form>

                    <hr>

                    {% for comment in comments %}
                    <div class="media">
                        <div class="media-left">
                            <img class="media-object" src="/static/img/default_avatar.png" alt="头像" width="40" height="40">
                        </div>
                        <div class="media-body">
                            <h4 class="media-heading">{{ comment.creator.username }} <small>{{ comment.create_datetime|date:'Y-m-d H:i' }}</small></h4>
                            <p>{{ comment.content }}</p>
                        </div>
                    </div>
                    <hr>
                    {% empty %}
                    <p class="text-center">暂无评论</p>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
    // 删除BUG确认
    $('.delete-btn').click(function() {
        var url = $(this).data('url');
        if (confirm('确定要删除这个BUG吗？')) {
            $.ajax({
                url: url,
                type: 'POST',
                data: {'csrfmiddlewaretoken': '{{ csrf_token }}'},
                success: function(data) {
                    if (data.status) {
                        window.location.href = '{% url 'bug_list' project_id=project.id %}';


                    } else {
                        alert(data.error);
                    }
                },
                error: function() {
                    alert('删除失败，请重试');
                }
            });
        }
    });

    // 更新BUG状态
    $('.status-select').change(function() {
        var url = $(this).data('url');
        var status_id = $(this).val();

        $.ajax({
            url: url,
            type: 'POST',
            data: {
                'csrfmiddlewaretoken': '{{ csrf_token }}',
                'status_id': status_id
            },
            success: function(data) {
                if (data.status) {
                    // 更新成功，可以刷新页面或更新状态显示
                    // 这里选择不刷新页面
                } else {
                    alert(data.error);
                }
            },
            error: function() {
                alert('更新状态失败，请重试');
            }
        });
    });
});
</script>
{% endblock %}